package cn.gene.timeyan.coo.webapi.controller;


import cn.gene.timeyan.common.restful.JsonPage;
import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.coo.service.CooperationService;
import cn.gene.timeyan.pojo.coo.dto.CooperationDTO;
import cn.gene.timeyan.pojo.coo.dto.CooperationDetailDTO;
import cn.gene.timeyan.pojo.coo.model.Cooperation;
import cn.gene.timeyan.pojo.coo.vo.CooperationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 浜掑姪璐翠俊鎭 前端控制器
 * </p>
 *
 * @author LWG
 * @since 2022-07-25
 */
@Slf4j
@RestController
@RequestMapping("/cooperation")
@Api(tags = "coo模块")
public class CooperationController {
    @Autowired(required = false)
    private CooperationService cooperationService;

    @GetMapping("/list/{userId}/{page}")
    @ApiOperation("获取用户发表的互助列表")
    public JsonResult<List<CooperationVO>> userArticleList(@PathVariable(value = "userId")Long userId,@PathVariable(value = "page") Integer page){
        return cooperationService.getUserHelpList(userId,page);
    }

    // 搜索模块最主要的功能就是实现搜索
    // 面对这种功能对应的控制器方法,可以不写任何路径
    // 按下面@GetMapping注解表示当前控制路径为localhost:10008/search
    @GetMapping("/coop")
    @ApiOperation("根据关键字查询ES中的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "pageNum",dataType = "int",
                    required = true),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",dataType = "int",
                    required = true)
    })
    public JsonResult<JsonPage<Cooperation>> getCooperationByPage(

            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "2") Integer pageSize
    ){
        JsonPage<Cooperation> list=cooperationService.getCooperationByPage(pageNum,pageSize);
        return JsonResult.ok(list);

    }
    @GetMapping("/lost")
    @ApiOperation("根据关键字查询ES中的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "pageNum",dataType = "int",
                    required = true),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",dataType = "int",
                    required = true)
    })
    public JsonResult<JsonPage<Cooperation>> getCooperationLostByPage(

            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "2") Integer pageSize
    ) {
        JsonPage<Cooperation> list = cooperationService.getCooperationLostByPage(pageNum, pageSize);
        return JsonResult.ok(list);
    }

    /**
     * cyn
     */

    @ApiOperation("求助功能")
    @PostMapping("/addNewCooperation")
    public JsonResult<Void> addNew(@Validated @RequestBody CooperationDTO cooperationDTO){
        log.info("dto====:{}",cooperationDTO);
        cooperationService.addNewCooperation(cooperationDTO);
        return JsonResult.ok();
    }


    @PostMapping("/detail")
    @ApiOperation("互助详情显示")
    public JsonResult<CooperationVO> cooperationDetail(@Validated @RequestBody CooperationDetailDTO cooperationDTO){
        CooperationVO cooperationVO=cooperationService.cooperationDetail(cooperationDTO);
        return JsonResult.ok(cooperationVO);
    }
    @GetMapping("/school/{modelId}/list/{school}")
    @ApiOperation("校园区互助列表显示")
    public JsonResult<List<CooperationVO>> list(@PathVariable(value = "school") String school, @PathVariable(value = "modelId") Long modelId){
        log.info("学校：{}, 模块id:{}",school,modelId);
        List<CooperationVO> list=cooperationService.getCooperationList(school,modelId);
        log.info("数据{}",list);
        return JsonResult.ok(list);
    }
    @GetMapping("/school/{modelId}/nextList")
    @ApiOperation("校园区互助列表再加载")
    public JsonResult<List<CooperationVO>> listNext(@RequestParam("school") String school, @PathVariable("modelId") Long modelId, @RequestParam("page") Integer page){
        List<CooperationVO> list=cooperationService.getHelpListNext(school,modelId,page);
        return JsonResult.ok(list);
    }
    @GetMapping("/country/{modelId}/list")
    @ApiOperation("全国区互助列表显示")
    public JsonResult<List<CooperationVO>> list(@PathVariable(value = "modelId") Long modelId){
        List<CooperationVO> list=cooperationService.getCountryCooperationList(modelId);
        return JsonResult.ok(list);
    }
    @GetMapping("/country/{modelId}/nextList")
    @ApiOperation("全国区互助列表再加载")
    public JsonResult<List<CooperationVO>> listNext(@PathVariable("modelId") Long modelId, @RequestParam("page") Integer page){
        List<CooperationVO> list=cooperationService.getCountryHelpListNext(modelId,page);
        return JsonResult.ok(list);
    }
}

